﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UCLibrary.UC
{
    public partial class UCRotor : UserControl
    {
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        public UCRotor()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            timer.Interval = 50;
            timer.Tick += timer_tick;
            timer.Start();
        }
        private int _speed = 1;
        public int speed
        {
            get { return _speed; }
            set { _speed = value; Refresh(); }
        }
        private bool _reversal = false;
        public bool Reversal
        {
            get { return _reversal; }
            set { _reversal = value; Refresh(); }
        }
        private void timer_tick(object sender, EventArgs e)
        {
            Angle += (-2*Convert.ToInt16(Reversal)+1)* speed;
            if(Math.Abs( Angle)>=360)
                Angle = (-2 * Convert.ToInt16(Reversal) + 1) * speed;
        }
        private int _angle = 0;
        public int Angle
        {
            get { return _angle; }
            set { _angle = value; Refresh(); }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Function.setHighQuality( g);
            GraphicsPath rotorPath = new GraphicsPath();
            int rotorWidthhalf = Width / 8;
            int rotorHeight = Height / 2;
            //rotorPath.AddEllipse(new RectangleF(rotorWidthhalf * 3, 0, 2 * rotorWidthhalf, rotorHeight));
            //rotorPath.AddEllipse(new RectangleF(rotorWidthhalf * 3, rotorHeight, 2 * rotorWidthhalf, rotorHeight - 1));
            //g.FillPath(new SolidBrush(Color.Green), rotorPath);
            //g.TranslateTransform(Width / 2, Height / 2);
            //g.RotateTransform(Angle);
            //g.TranslateTransform(-Width / 2, -Height / 2);
            rotorPath.AddClosedCurve(new Point[]
            {
                new Point(Width/2, 0),
                new Point(3*rotorWidthhalf, Height/4),
                new Point(Width/2,Height/2),
                new Point(3*rotorWidthhalf,3*Height/4),
                new Point(Width/2,Height),
                new Point(5*rotorWidthhalf,3*Height/4),
                new Point(Width/2,Height/2),
                new Point(5*rotorWidthhalf,Height/4)
            }); 
            Matrix matrix = new Matrix();
            matrix.RotateAt(Angle, new PointF(Width / 2, Height / 2));
            rotorPath.Transform(matrix);
            matrix.Dispose();
            //g.FillClosedCurve(Brushes.Red, new Point[]
            //{
            //    new Point(Width/2, 0),
            //    new Point(3*rotorWidthhalf, Height/4),
            //    new Point(Width/2,Height/2),
            //    new Point(3*rotorWidthhalf,3*Height/4),
            //    new Point(Width/2,Height),
            //    new Point(5*rotorWidthhalf,3*Height/4),
            //    new Point(Width/2,Height/2),
            //    new Point(5*rotorWidthhalf,Height/4)
            //}) ;
            g.FillPath(Brushes.Green,rotorPath);
            Region = new Region(rotorPath);
            //rotorPath.Dispose();
            base.OnPaint(e);
        }
    }
}
